﻿@model System_Role
@{
    ViewBag.Title = "权限设置";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<style>
    ul.ztree {
        width: auto;
        height: 395px;
        margin: 0;
    }
</style>
<script src="~/Content/js/zTree_v3/js/jquery.ztree.all-3.5.min.js"></script>
<div class="main-container">
    <div class="modal-form">
        <div class="modal-form-body">
            <form id="myform" action="/RolePower/PowerManage" class="form-horizontal" method="post">
                <ul id="treeDemo" class="ztree" style="background-color: white"></ul>
                <input type="hidden" name="RoledList" id="RoledList" value="" />
                <input type="hidden" name="roleId" id="roleId" value="@ViewData["roleId"]" />
                <input type="hidden" name="ids" id="ids" value="@ViewData["ids"]" />
            </form>
        </div>
        <div class="modal-form-footer">
            <input type="button" class="btn-primary" value="保存" onclick="autoSubmit()" />
            <input type="button" class="btn-secondary" value="取消" onclick="CloseModelFrame()">
        </div>
    </div>
</div>
<script type="text/javascript">
    //组织树开始
    var roleId = $('#roleId').val();
    var url = "@Url.Action("GetMenuTreeJson", "System")?roleId=" + roleId;
    var setting = {
        async: {
            enable: true,
            url: url,
            autoParam: ["id", "pId", "name"]
        },
        check: {
            enable: true,
            chkboxType: { "Y": "ps", "N": "ps" }
        },
        view: {
            dblClickExpand: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onAsyncSuccess: zTreeOnAsyncSuccess
        }
    };

    function beforeClick(treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        zTree.checkNode(treeNode, !treeNode.checked, null, true);
        return false;
    }
    function zTreeOnAsyncSuccess() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        var te = $("#ids").val();
        var list = te.split(",");
        for (var i = 0; i < list.length; i++) {
            var node = zTree.getNodeByParam("id", list[i], null);
            if (node != null) {
                zTree.checkNode(node, true, false);
            }
        }
        zTree.expandAll(true);
    }
    $(document).ready(function () {
        $.fn.zTree.init($("#treeDemo"), setting);
    });

    function autoSubmit() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = zTree.getCheckedNodes();
        if (nodes[0] == null) {
            layer.msg('请选择权限！', { icon: 5, time: 1000, shade: 0.3 });
            return;
        }
        var arrRoleId = [];
        $.each(nodes, function (i, obj) {
            arrRoleId.push(obj.id);
        });
        //var roledList = '';
        //for (var i = 0; i < nodes.length; i++) {
        //    if (roledList != '') roledList += ',';
        //    roledList += nodes[i].id;
        //}
        var RuledList = escape(RuledList);
        var roleId = $('#roleId').val();
        var url = "@Url.Action("PowerManageSave", "System")?roledList=" + arrRoleId.join(',') + "&roleId=" + roleId;

        var options = {
            type: 'POST',
            //data:postData,
            url: url,
            success: showResponse
        };
        if ($("#myform").validate().form()) {
            $.ajax(options);
        }
    }
    function showResponse(msg) {
        if (msg == 'y') {
            parent.layer.msg('保存成功！', { icon: 1, time: 700 }, function () {
                window.parent.location.reload(true);
            });
        } else {
            parent.layer.msg('添加失败！', { icon: 2 });
        }
    }
    $(document).ready(function () {
        $("#myform").validate({
            rules: {
                roleId: { required: true }
            },
            messages: {
                roleId: {
                    required: "请选择权限"
                }
            }
        });
    });
</script>
